package test3;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class Send {
    //发送端
    public Send(String str) throws Exception {
        //1.使用DatagramSocket,指定端口,建立发送端
        DatagramSocket c = new DatagramSocket(8080);//端口号
        //2.准备数据,将数据转为字节数组
        byte[] s = str.getBytes();
        //3.封装成DatagramPacket包裹,需要指定数据
        DatagramPacket p = new DatagramPacket(s, s.length, new InetSocketAddress("localhost", 8081));//发过去的位置
        //4.发送包裹
        c.send(p);
        //释放资源
        c.close();
    }

}
